package com.itheima.web;

import com.itheima.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/userList")
public class UserListServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      //请求方式保持一致  所以走的 dopost
        //从域中取出数据进行展示
       List<User> list = (List<User>) request.getAttribute("list");
//处理中文乱码
       response.setContentType("text/html;charset=utf-8");
       //你怎么展示
        PrintWriter writer = response.getWriter();

        writer.write("<table border=\"1px\" cellspacing=\"0\" cellpadding=\"0\" width=\"500px\" align=\"center\">\n" +
                "        <tr>\n" +
                "            <td>用户id</td>\n" +
                "            <td>用户名</td>\n" +
                "            <td>email</td>\n" +
                "            <td>姓名</td>\n" +
                "            <td>性别</td>\n" +
                "        </tr>");

        for (User user : list) {
            writer.write(" <tr>\n" +
                    "           <td>"+user.getUid()+"</td>\n" +
                    "           <td>"+user.getUsername()+"</td>\n" +
                    "           <td>"+user.getEmail()+"</td>\n" +
                    "           <td>"+user.getName()+"</td>\n" +
                    "           <td>"+user.getSex()+"</td>\n" +
                    "       </tr>");
        }

        writer.write("</table>");

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
